﻿
using System;
namespace HBase {
    public class TimeUtil {
        public static int step = 0;


        public static int getBit(int v ,int bit) {
            return (v >> bit) & 1;
        }


        /// <summary>
        /// 时间戳转DateTime
        /// </summary>
        /// <returns></returns>
        public static DateTime TimeStamp2DT(string timeStamp) {
            return DateTime.Parse("1970-01-01 00:00:00").AddSeconds(long.Parse(timeStamp));
        }
        public static DateTime TimeStamp2DT(long timeStamp) {
            return DateTime.Parse("1970-01-01 00:00:00").AddSeconds(timeStamp);
        }
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp(System.DateTime time) {
            return ConvertDateTimeToInt(time);
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        /// 
        public static DateTime baseTime = DateTime.Parse("1970-01-01 00:00:00");
        public static long ConvertDateTimeToInt(System.DateTime time) {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //long t = (time.Ticks - startTime.Ticks) / 10000000;   //除10000调整为13位      
            //return t;
            TimeSpan sp = time - baseTime;
            return ( long ) sp.TotalMilliseconds;
        }
        public static string seconds2DateSring(object timeStamp) {
            return seconds2Sring(timeStamp ,"M-dd HH:mm:ss");
        }
        public static string seconds2TimeSring(object timeStamp) {
            return seconds2Sring(timeStamp ,"HH:mm:ss");
        }
        private static string seconds2Sring(object timeStamp ,String format) {
            if( timeStamp.GetType() == typeof(String) ) {
                DateTime dt = TimeStamp2DT(( String ) timeStamp);
                return dt.ToString(format);
            } else if( timeStamp.GetType() == typeof(DateTime) ) {
                return (( DateTime ) timeStamp).ToString(format);
            } else {
                return timeStamp.ToString();
            }
        }
        public static long date2Long(DateTime t) {
            long d = t.Year;
            d +=100* t.Month;    
            d +=100* t.Day;
            d+=100* t.Hour; 
            d+=100* t.Minute;   
            d+=100* t.Second; 
            return d;
        }

        public static DateTime long2date(long t) {
       
            int s = (int )(t % 100);
            t = t / 100;
            int m = (int)(t % 100);
            t = t / 100;
            int h= (int)(t % 100);
            t = t / 100;
            int d = (int)(t % 100);
            t = t / 100;
            int M= (int)(t % 100);
            t = t / 100;
            int y = (int)(t % 100);
            DateTime time = new DateTime(y,M,d,h,m,s);
            return time;
        }
        public static bool cross(DateTime b1 ,DateTime e1 ,DateTime b2 ,DateTime e2) {

            if( b1 <= b2 && b2 < e1 ) {
                return true;
            }

            if( b1 < e2 && e2 <= e1 ) {
                return true;
            }

            if( b2 < e1 && e1 <= e2 ) {
                return true;
            }
            return false;
        }



    }
}
